#define _XOPEN_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "traitement.h"
#include "config/config.h"
#include "real_path.h"


requete_t
creer_requete()
{
  
  requete_t self=malloc(sizeof(struct requete_t));
 
  self->methode=pas_methode;
  self->protocole=pas_protocole;
  self->entete=pas_entete;
  self->url= NULL;

  return self;
}

void
liberer_requete(requete_t self)
{
  if (self->url != NULL)
    {
      free(self->url);
      self->url = NULL;
    }
  free(self);
  self = NULL;
}

requete_t
traiter_requete(char *requete_recue)
{
  requete_t resultat = creer_requete();
  
  char methode[5];
  char *url;


  int i = 0;
  for (; i < 5 && requete_recue[i] != ' ';i++)//methode delete pas geree, les commandes 
                                              //depassent pas 4 carac (head, post, get)
    ;
  
  if (requete_recue[i] != ' ')
    return resultat;
  //mauvaise requete

  if (sscanf(requete_recue,"%s ",methode) != 1)
    return resultat; //probleme

  printf("methode trouvee = %s\n",methode);
  
  int j ;

  for (j = i+1; requete_recue[j]!= '\n' && requete_recue[j] != ' ' && requete_recue[j] != '\0' ; j++)
    ;
  
  int taille_url = j - (i +1);

  printf("taille url = %d\n",taille_url);

  url = malloc(sizeof(char) *(taille_url+1));

  strncpy(url,requete_recue+i+1,sizeof(char)* taille_url);
  url[taille_url]='\0';
  printf("url recup = %s\n",url);
 
  char *vraipath = chemin_correct(url);
  if (vraipath == NULL)//chemin non trouve
    return resultat;

  if (url)
    free(url);
  url = NULL;

  printf("Methode = %s\nurl = %s\n",methode, vraipath);

  

  if (strcmp(methode,"GET") == 0)
    resultat->methode = get;
  else
    if (strcmp(methode,"HEAD") == 0)
      resultat->methode = head;
    else
      if (strcmp(methode,"POST") == 0)
	resultat->methode = post;
      else
	return resultat;

  resultat->url = malloc(sizeof(char)*(strlen(vraipath)+1));
  strcpy(resultat->url, vraipath);


 
  return resultat;


}

  
int
protocole_09(char *premiere_ligne)
{//A PASSER DANS UN .H LA CHAINE DE CARACTERES §§
  return (strstr(premiere_ligne,"HTTP/1.0") == NULL &&
	  strstr(premiere_ligne,"HTTP/1.1") == NULL);

}

char *
recupere_requete(int fdsock)
{
  char  buffer[BUFFER_SIZE];
  int   nb_read;
  int   size_requete = BUFFER_SIZE;
  printf("avant\n");
  char *requete = malloc(sizeof(char)*size_requete);
  printf("apres\n");
  int off = 0;

  while ((nb_read = read(fdsock,buffer, BUFFER_SIZE - 1)) > 0)
    {
      
      if (size_requete < off + nb_read)
	{
	  size_requete <<= 1;
	  requete = realloc(requete,sizeof(char)*size_requete);
	}


      buffer[nb_read] = '\0';
      strcpy(requete + off, buffer);
      off += nb_read;

      if (strchr(requete,'\n') != NULL && protocole_09(requete))//on detecte si cest le protocole 0.9 
	                                                        //utilise en regardant la premiere ligne
	break;

      if (strlen(requete) > 2 &&
	  (requete[strlen(requete)-2] == '\n' || requete[strlen(requete)-2] == '\r') && 
	   requete[strlen(requete)-1] == '\n')  //pour reperer fin de requete
	{
	  
	  break;
	}    
    }

  
  requete = realloc(requete,sizeof(char)*(strlen(requete)+1));
  printf("requete recup.%s \n",requete);
  return requete;
}
  


/*int
main(int argc, char *argv[])
{
   char buffer[4095];
  int r;
  
  while ((r = read(0,buffer,sizeof(buffer))) != 0)
    {
      char path[1024];
      int req = traiter_requete(buffer,path);
      
      printf("requete = %d\npath = %s\n",req,path);

      }
 
  return EXIT_SUCCESS;
}*/

